package com.fly.estore.dao.Impl;

import com.fly.estore.dao.AccountDAO;
import com.fly.estore.dao.BaseDAO;
import com.fly.estore.domain.AccountInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fly on 2016/3/25.
 */
@Repository("AccountDAO")
public class AccountDAOImpl extends BaseDAO<AccountInfo> implements AccountDAO {

    public int create(final AccountInfo accountInfo) {
        StringBuilder sql = new StringBuilder("insert into account_info(account_id, account_name, password, mobile, header_pic, account_status, create_id, create_time)");
        sql.append(" values(?, ?, ?, ?, ?, ?, ?, ?);");
        int update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, accountInfo.getAccountId());
                ps.setString(i++, accountInfo.getAccountName());
                ps.setString(i++, accountInfo.getPassword());
                ps.setString(i++, accountInfo.getMobile());
                ps.setString(i++, accountInfo.getHeaderPic());
                ps.setString(i++, accountInfo.getAccountStatus());
                ps.setString(i++, accountInfo.getCreateId());
                ps.setString(i++, accountInfo.getCreateTime());
            }
        });
        return update;
    }

    public int update(final AccountInfo accountInfo) {
        StringBuilder sql = new StringBuilder();
        sql.append("update account_info")
                .append(" set account_name = ?, ")
                .append(" password = ?,")
                .append(" mobile = ?,")
                .append(" header_pic = ?,")
                .append(" account_status = ?,")
                .append(" last_update_id = ?,")
                .append(" last_update_time = ?")
                .append(" where account_id = ?");
        int update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, accountInfo.getAccountName());
                ps.setString(i++, accountInfo.getPassword());
                ps.setString(i++, accountInfo.getMobile());
                ps.setString(i++, accountInfo.getHeaderPic());
                ps.setString(i++, accountInfo.getAccountStatus());
                ps.setString(i++, accountInfo.getLastUpdateId());
                ps.setString(i++, accountInfo.getLastUpdateTime());
                ps.setString(i++, accountInfo.getAccountId());
            }
        });
        return update;
    }

    public int deleteById(final String accountId) {
        StringBuilder sql = new StringBuilder();
        sql.append("delete from account_info where account_id = ?");
        int update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, accountId);
            }
        });
        return update;
    }

    public AccountInfo getById(String accountId) {
        AccountInfo accountInfo = null;
        StringBuilder sql = new StringBuilder("select * from account_info where account_id = ?");
        List<AccountInfo> resultList = getJdbcTemplate().query(sql.toString(), new AccountRowMapper(), new Object[]{ accountId});
        if (!CollectionUtils.isEmpty(resultList)){
            accountInfo = resultList.get(0);
        }
        return accountInfo;
    }

    public List<AccountInfo> getList() {
        List<AccountInfo> accountList = new ArrayList<AccountInfo>();
        String sql = "select * from account_info where account_name <> 'admin'";
        accountList = getJdbcTemplate().query(sql, new AccountRowMapper());
        return accountList;
    }

    public AccountInfo getByAccountNameAndPwd(String accountName, String password) {
        AccountInfo accountInfo = null;
        StringBuilder sql = new StringBuilder("select * from account_info where account_name = ? and password = ?");
        List<AccountInfo> resultList = getJdbcTemplate().query(sql.toString(), new AccountRowMapper(), new Object[]{accountName, password});
        if (!CollectionUtils.isEmpty(resultList)){
            accountInfo = resultList.get(0);
        }
        return accountInfo;
    }

    public class AccountRowMapper implements RowMapper<AccountInfo> {
        public AccountInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
            AccountInfo accountInfo = new AccountInfo();
            accountInfo.setAccountId(rs.getString("account_id"));
            accountInfo.setAccountName(rs.getString("account_name"));
            accountInfo.setPassword(rs.getString("password"));
            accountInfo.setMobile(rs.getString("mobile"));
            accountInfo.setHeaderPic(rs.getString("header_pic"));
            accountInfo.setAccountStatus(rs.getString("account_status"));
            accountInfo.setCreateId(rs.getString("create_id"));
            accountInfo.setCreateTime(rs.getString("create_time"));
            accountInfo.setLastUpdateId(rs.getString("last_update_id"));
            accountInfo.setLastUpdateTime(rs.getString("last_update_time"));
            return accountInfo;
        }
    }

}
